package com.aop2.JDKProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy implements InvocationHandler {

    private Object targetObj;

    public Object createProxy(Object targetObj) {
        this.targetObj = targetObj;
        ClassLoader classLoader = targetObj.getClass().getClassLoader();
        Class[] interfaces = targetObj.getClass().getInterfaces();
        return Proxy.newProxyInstance(classLoader, interfaces, this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MyAspect aspect = new MyAspect();
        aspect.checkPermission();
        Object obj = method.invoke(targetObj, args);
        aspect.log();
        return obj;
    }
}
